#!/bin/bash

IFS_BAK=$IFS
IFS="
"

inv="/Data/inventory"
invT="/Data/inventory/temp"
mediaA="/Data/MovieA"
mediaB="/Data/MovieB"
mediaT1="/Data/MovieT2"
mediaT2="/Data/MovieT2"
/bin/ls -1 $mediaA > $invT/movielistA
/bin/ls -1 $mediaB > $invT/movielistB
/bin/ls -1 $mediaT1 > $invT/movielistT1
/bin/ls -1 $mediaT2 > $invT/movielistT2
/bin/echo "Title;Year;Release Date;Studio;Director;Rating;Duration (Min);Resolution Size;Audio Channel;Audio Codec;Video Codec;File Extention;File Size Human Readable;File Size(kb);File Location;Sub Title File;Set Collection"  > $inv/movielist.csv

for fnA in `/bin/cat $invT/movielistA`; do
lf=`/usr/bin/find $mediaA'/'$fnA -size +50M -exec ls -lh {} \+ |cut -d "/" -f 2-`
/usr/bin/mplayer -vo null -ao null -frames 0 -identify '/'$lf | /bin/grep "^ID" > $invT/movieatb
name=`/bin/cat $invT/movieatb | /bin/grep "^ID_FILENAME" | /usr/bin/cut -d '=' -f 2`
fn_sub=`/bin/cat $invT/movieatb | /bin/grep "^ID_FILE_SUB_FILENAME" | /usr/bin/cut -d '=' -f 2`
dem=`/bin/cat $invT/movieatb | /bin/grep "^ID_DEMUXER" | /usr/bin/cut -d '=' -f 2`
vfor=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_FORMAT" | /usr/bin/cut -d '=' -f 2`
media_wth=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_WIDTH" | /usr/bin/cut -d '=' -f 2`
media_hgt=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_HEIGHT" | /usr/bin/cut -d '=' -f 2`
afor=`/bin/cat $invT/movieatb | /bin/grep "^ID_AUDIO_FORMAT" | /usr/bin/cut -d '=' -f 2`
len=`/bin/cat $invT/movieatb | /bin/grep "^ID_LENGTH" | /usr/bin/cut -d '=' -f 2`
media_vcode=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_CODEC" | /usr/bin/cut -d '=' -f 2`
media_ach=`/bin/cat $invT/movieatb | /bin/grep "^ID_AUDIO_NCH" | /usr/bin/head -1 | /usr/bin/cut -d '=' -f 2`
media_acode=`/bin/cat $invT/movieatb | /bin/grep "^ID_AUDIO_CODEC" | /usr/bin/cut -d '=' -f 2`
fsize_kb=`/usr/bin/du -l "$name"|/usr/bin/cut -d '/' -f 1`
fsize_hr=`/usr/bin/du -lh "$name"|/usr/bin/cut -d '/' -f 1`
fex=`/bin/echo "$name" |/usr/bin/cut -d '/' -f 2- |/bin/sed 's/.*\(...\)$/\1/'` 
dura=`/bin/echo  "$len/60" | /usr/bin/bc`
media_loc=`/bin/echo "$name" | /usr/bin/rev |/usr/bin/cut -c 5- |/usr/bin/rev`
media_tle=`/bin/echo "$media_loc" | /usr/bin/cut -d '/' -f 5 | /usr/bin/rev |/usr/bin/cut -c 8- |/usr/bin/rev`
media_yr=`/bin/echo "$media_loc" |/usr/bin/cut -d '/' -f 5 |/bin/sed 's/.*\(.....\)$/\1/'|/usr/bin/cut -d ')' -f 1`
box=`/usr/bin/xml_grep set --text_only $media_loc.nfo`
rdate=`/usr/bin/xml_grep releasedate --text_only  $media_loc.nfo`
studio=`/usr/bin/xml_grep studio --text_only  $media_loc.nfo`
director=`/usr/bin/xml_grep director --text_only  $media_loc.nfo`
rating=`/usr/bin/xml_grep rating --text_only  $media_loc.nfo`
/bin/echo ""$media_tle";"$media_yr";"$rdate";"$studio";"$director";"$rating";"$dura";"$media_wth x $media_hgt";"$media_ach";"$media_acode";"$vfor";"$fex";"$fsize_hr";"$fsize_kb";"$media_loc";"$fn_sub";"$box"" >> $inv/movielist.csv
done 

for fnB in `/bin/cat $invT/movielistB`; do
lf=`/usr/bin/find $mediaB'/'$fnB -size +50M -exec ls -lh {} \+ |cut -d "/" -f 2-`
/usr/bin/mplayer -vo null -ao null -frames 0 -identify '/'$lf | /bin/grep "^ID" > $invT/movieatb
name=`/bin/cat $invT/movieatb | /bin/grep "^ID_FILENAME" | /usr/bin/cut -d '=' -f 2`
fn_sub=`/bin/cat $invT/movieatb | /bin/grep "^ID_FILE_SUB_FILENAME" | /usr/bin/cut -d '=' -f 2`
dem=`/bin/cat $invT/movieatb | /bin/grep "^ID_DEMUXER" | /usr/bin/cut -d '=' -f 2`
vfor=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_FORMAT" | /usr/bin/cut -d '=' -f 2`
media_wth=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_WIDTH" | /usr/bin/cut -d '=' -f 2`
media_hgt=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_HEIGHT" | /usr/bin/cut -d '=' -f 2`
afor=`/bin/cat $invT/movieatb | /bin/grep "^ID_AUDIO_FORMAT" | /usr/bin/cut -d '=' -f 2`
len=`/bin/cat $invT/movieatb | /bin/grep "^ID_LENGTH" | /usr/bin/cut -d '=' -f 2`
media_vcode=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_CODEC" | /usr/bin/cut -d '=' -f 2`
media_ach=`/bin/cat $invT/movieatb | /bin/grep "^ID_AUDIO_NCH" | /usr/bin/head -1 | /usr/bin/cut -d '=' -f 2`
media_acode=`/bin/cat $invT/movieatb | /bin/grep "^ID_AUDIO_CODEC" | /usr/bin/cut -d '=' -f 2`
fsize_kb=`/usr/bin/du -l "$name"|/usr/bin/cut -d '/' -f 1`
fsize_hr=`/usr/bin/du -lh "$name"|/usr/bin/cut -d '/' -f 1`
fex=`/bin/echo "$name" |/usr/bin/cut -d '/' -f 2- |/bin/sed 's/.*\(...\)$/\1/'`
dura=`/bin/echo  "$len/60" | /usr/bin/bc`
media_loc=`/bin/echo "$name" | /usr/bin/rev |/usr/bin/cut -c 5- |/usr/bin/rev`
media_tle=`/bin/echo "$media_loc" | /usr/bin/cut -d '/' -f 5 | /usr/bin/rev |/usr/bin/cut -c 8- |/usr/bin/rev`
media_yr=`/bin/echo "$media_loc" |/usr/bin/cut -d '/' -f 5 |/bin/sed 's/.*\(.....\)$/\1/'|/usr/bin/cut -d ')' -f 1`
box=`/usr/bin/xml_grep set --text_only $media_loc.nfo`
rdate=`/usr/bin/xml_grep releasedate --text_only  $media_loc.nfo`
studio=`/usr/bin/xml_grep studio --text_only  $media_loc.nfo`
director=`/usr/bin/xml_grep director --text_only  $media_loc.nfo`
rating=`/usr/bin/xml_grep rating --text_only  $media_loc.nfo`
/bin/echo ""$media_tle";"$media_yr";"$rdate";"$studio";"$director";"$rating";"$dura";"$media_wth x $media_hgt";"$media_ach";"$media_acode";"$vfor";"$fex";"$fsize_hr";"$fsize_kb";"$media_loc";"$fn_sub";"$box"" >> $inv/movielist.csv
done

for fnT in `/bin/cat $invT/movielistT1`; do
lf=`/usr/bin/find $mediaT1'/'$fnT -size +50M -exec ls -lh {} \+ |cut -d "/" -f 2-`
/usr/bin/mplayer -vo null -ao null -frames 0 -identify '/'$lf | /bin/grep "^ID" > $invT/movieatb
name=`/bin/cat $invT/movieatb | /bin/grep "^ID_FILENAME" | /usr/bin/cut -d '=' -f 2`
fn_sub=`/bin/cat $invT/movieatb | /bin/grep "^ID_FILE_SUB_FILENAME" | /usr/bin/cut -d '=' -f 2`
dem=`/bin/cat $invT/movieatb | /bin/grep "^ID_DEMUXER" | /usr/bin/cut -d '=' -f 2`
vfor=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_FORMAT" | /usr/bin/cut -d '=' -f 2`
media_wth=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_WIDTH" | /usr/bin/cut -d '=' -f 2`
media_hgt=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_HEIGHT" | /usr/bin/cut -d '=' -f 2`
afor=`/bin/cat $invT/movieatb | /bin/grep "^ID_AUDIO_FORMAT" | /usr/bin/cut -d '=' -f 2`
len=`/bin/cat $invT/movieatb | /bin/grep "^ID_LENGTH" | /usr/bin/cut -d '=' -f 2`
media_vcode=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_CODEC" | /usr/bin/cut -d '=' -f 2`
media_ach=`/bin/cat $invT/movieatb | /bin/grep "^ID_AUDIO_NCH" | /usr/bin/head -1 | /usr/bin/cut -d '=' -f 2`
media_acode=`/bin/cat $invT/movieatb | /bin/grep "^ID_AUDIO_CODEC" | /usr/bin/cut -d '=' -f 2`
fsize_kb=`/usr/bin/du -l "$name"|/usr/bin/cut -d '/' -f 1`
fsize_hr=`/usr/bin/du -lh "$name"|/usr/bin/cut -d '/' -f 1`
fex=`/bin/echo "$name" |/usr/bin/cut -d '/' -f 2- |/bin/sed 's/.*\(...\)$/\1/'`
dura=`/bin/echo  "$len/60" | /usr/bin/bc`
media_loc=`/bin/echo "$name" | /usr/bin/rev |/usr/bin/cut -c 5- |/usr/bin/rev`
media_tle=`/bin/echo "$media_loc" | /usr/bin/cut -d '/' -f 5 | /usr/bin/rev |/usr/bin/cut -c 8- |/usr/bin/rev`
media_yr=`/bin/echo "$media_loc" |/usr/bin/cut -d '/' -f 5 |/bin/sed 's/.*\(.....\)$/\1/'|/usr/bin/cut -d ')' -f 1`
box=`/usr/bin/xml_grep set --text_only $media_loc.nfo`
rdate=`/usr/bin/xml_grep releasedate --text_only  $media_loc.nfo`
studio=`/usr/bin/xml_grep studio --text_only  $media_loc.nfo`
director=`/usr/bin/xml_grep director --text_only  $media_loc.nfo`
rating=`/usr/bin/xml_grep rating --text_only  $media_loc.nfo`
/bin/echo ""$media_tle";"$media_yr";"$rdate";"$studio";"$director";"$rating";"$dura";"$media_wth x $media_hgt";"$media_ach";"$media_acode";"$vfor";"$fex";"$fsize_hr";"$fsize_kb";"$media_loc";"$fn_sub";"$box"" >> $inv/movielist.csv
done

for fnT2 in `/bin/cat $invT/movielistT2`; do
lf=`/usr/bin/find $mediaT2'/'$fnT2 -size +50M -exec ls -lh {} \+ |cut -d "/" -f 2-`
/usr/bin/mplayer -vo null -ao null -frames 0 -identify '/'$lf | /bin/grep "^ID" > $invT/movieatb
name=`/bin/cat $invT/movieatb | /bin/grep "^ID_FILENAME" | /usr/bin/cut -d '=' -f 2`
fn_sub=`/bin/cat $invT/movieatb | /bin/grep "^ID_FILE_SUB_FILENAME" | /usr/bin/cut -d '=' -f 2`
dem=`/bin/cat $invT/movieatb | /bin/grep "^ID_DEMUXER" | /usr/bin/cut -d '=' -f 2`
vfor=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_FORMAT" | /usr/bin/cut -d '=' -f 2`
media_wth=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_WIDTH" | /usr/bin/cut -d '=' -f 2`
media_hgt=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_HEIGHT" | /usr/bin/cut -d '=' -f 2`
afor=`/bin/cat $invT/movieatb | /bin/grep "^ID_AUDIO_FORMAT" | /usr/bin/cut -d '=' -f 2`
len=`/bin/cat $invT/movieatb | /bin/grep "^ID_LENGTH" | /usr/bin/cut -d '=' -f 2`
media_vcode=`/bin/cat $invT/movieatb | /bin/grep "^ID_VIDEO_CODEC" | /usr/bin/cut -d '=' -f 2`
media_ach=`/bin/cat $invT/movieatb | /bin/grep "^ID_AUDIO_NCH" | /usr/bin/head -1 | /usr/bin/cut -d '=' -f 2`
media_acode=`/bin/cat $invT/movieatb | /bin/grep "^ID_AUDIO_CODEC" | /usr/bin/cut -d '=' -f 2`
fsize_kb=`/usr/bin/du -l "$name"|/usr/bin/cut -d '/' -f 1`
fsize_hr=`/usr/bin/du -lh "$name"|/usr/bin/cut -d '/' -f 1`
fex=`/bin/echo "$name" |/usr/bin/cut -d '/' -f 2- |/bin/sed 's/.*\(...\)$/\1/'`
dura=`/bin/echo  "$len/60" | /usr/bin/bc`
media_loc=`/bin/echo "$name" | /usr/bin/rev |/usr/bin/cut -c 5- |/usr/bin/rev`
media_tle=`/bin/echo "$media_loc" | /usr/bin/cut -d '/' -f 5 | /usr/bin/rev |/usr/bin/cut -c 8- |/usr/bin/rev`
media_yr=`/bin/echo "$media_loc" |/usr/bin/cut -d '/' -f 5 |/bin/sed 's/.*\(.....\)$/\1/'|/usr/bin/cut -d ')' -f 1`
box=`/usr/bin/xml_grep set --text_only $media_loc.nfo`
rdate=`/usr/bin/xml_grep releasedate --text_only  $media_loc.nfo`
studio=`/usr/bin/xml_grep studio --text_only  $media_loc.nfo`
director=`/usr/bin/xml_grep director --text_only  $media_loc.nfo`
rating=`/usr/bin/xml_grep rating --text_only  $media_loc.nfo`
/bin/echo ""$media_tle";"$media_yr";"$rdate";"$studio";"$director";"$rating";"$dura";"$media_wth x $media_hgt";"$media_ach";"$media_acode";"$vfor";"$fex";"$fsize_hr";"$fsize_kb";"$media_loc";"$fn_sub";"$box"" >> $inv/movielist.csv
done




echo "DONE Generating - `date`"
IFS=$IFS_BAK
IFS_BAK=
